import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/major_hazard/hazard_obligation_mission_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/major_hazard/hazard_obligation_person_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/major_hazard_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/obligation/obligation_type.dart';

class HazardObligationsController extends GetxController {
  HazardObligationsController();

  HazardObligationType type = HazardObligationType.main;

  MajorHazardController get hazardController => Get.find<MajorHazardController>();

  final persons = <HazardObligationPersonEntity>[].obs;

  final missions = <HazardObligationMissionEntity>[].obs;

  Future<List<HazardObligationPersonEntity>?> getPersons() async {
    persons.clear();
    final res = await HttpUtil().generateGetRequest<List<HazardObligationPersonEntity>>(
      Interface.getHazardPersonRelationship,
      queryParameters: {
        'hazardCode': hazardController.selectedHazard?.id,
        'workType': type.id,
      },
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        persons.value = result.data ?? [];
        update();
      }
    });
    return res.data;
  }

  Future<List<HazardObligationMissionEntity>?> getMissions() async {
    missions.clear();
    final res = await HttpUtil().generateGetRequest<List<HazardObligationMissionEntity>?>(
      Interface.getHazardMeasuresList,
      queryParameters: {
        'workType': type.id,
        'hazardCode': hazardController.selectedHazard?.id,
      },
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        missions.value = result.data ?? [];
        update();
      }
    });
    return res.data;
  }
}
